﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GongXianDCUI.Models;
using GongXianDCUI.DAL;

namespace GongXianDCUI.BLL
{
    public class FunctionsBLL
    {
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        public List<Funcitions> GetFunctionList()
        {
            FunctionsDAL dal = new FunctionsDAL();
            //获取根节点
            List<Funcitions> rootList = dal.GetFuncitionsList(0);
            //获取根节点,首页头部不展示的根节点
            List<Funcitions> rootList2 = dal.GetFuncitionsList(null);
            rootList.AddRange(rootList2);

            //获取跟节点的子集
            for (int i = 0; i < rootList.Count; i++)
            {
                List<Funcitions> childrens = dal.GetFuncitionsList(rootList[i].FunctionId);
                if (childrens != null)
                {
                    rootList[i].Childrens = childrens;
                }
            }
            return rootList;
        }


        /// <summary>
        /// 查询菜单集合
        /// </summary>
        /// <returns></returns>
        public List<Funcitions> GetFuncitionsList(int? parentId)
        {
            return DB.Context.From<Funcitions>().Where(w => w.ParentId == parentId).ToList();
        }

        public Funcitions getFun(int? id)
        {
            return DB.Context.From<Funcitions>().Where(p => p.FunctionId == id).ToFirst();
        }
    }
}
